package com.example.yiwei.crm.view.activity.coordinationActivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.bigkoo.pickerview.TimePickerView;
import com.example.yiwei.crm.R;
import com.example.yiwei.crm.base.BaseActivity;
import com.example.yiwei.crm.base.baseClass;
import com.example.yiwei.crm.dao.dockDao;
import com.example.yiwei.crm.dao_impl.dockDaoImpl;
import com.example.yiwei.crm.data.GetCoorData;
import com.example.yiwei.crm.entity.BMEntity;
import com.example.yiwei.crm.entity.CoorSeacherVisitActivity;
import com.example.yiwei.crm.json.FZJson;
import com.example.yiwei.crm.util.NetWorkUtils;
import com.example.yiwei.crm.util.SC;
import com.example.yiwei.crm.view.activity.JYCoordingActivity.SimpleCoordingActivity1;
import com.example.yiwei.crm.view.activity.jtCustomer.JDcoorActivity;

import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class SearchCoordinationActivity extends BaseActivity {
    private EditText edit_name,edit_jg,edit_bm,edit_pnm,edit_cid,edit_manger;
    private ArrayAdapter fzadaper,bmadaper;
    private SharedPreferences sp;
    List<BMEntity> fzlist;
    List<BMEntity> abmlist;
    List<BMEntity> bmlist;
    List<String> fzList;
    List<String> bmList;
    String fzbianma = null;
    private ImageView img_commit,img_ack,img_update;
    String bmbianma = null;
    private TimePickerView mpv1;
    private TimePickerView mpv2;
    Date date1,date2,date3,date4;
    String stime;
    String etime;
    TextView stimetv,etimetv;
    private Button btn1,btn2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_coordination);
        stimetv = (TextView) findViewById(R.id.start_time);
        etimetv = (TextView) findViewById(R.id.end_time);
        btn1= (Button) findViewById(R.id.start_show);
        btn2= (Button) findViewById(R.id.end_show);
        edit_name = (EditText) findViewById(R.id.health_education_add_edit_docking_name);
        edit_jg = (EditText) findViewById(R.id.health_education_add_edit_docking_jg);
        edit_bm = (EditText) findViewById(R.id.health_education_add_edit_docking_bm);
        edit_pnm = (EditText) findViewById(R.id.health_education_add_edit_docking_pnm);
        edit_cid = (EditText) findViewById(R.id.health_education_add_edit_docking_cname);
        img_update= (ImageView) findViewById(R.id.detailed_doc_img_update);
        edit_manger = (EditText) findViewById(R.id.health_education_add_edit_docking_manger);
        img_commit= (ImageView) findViewById(R.id.coording_search_img_commit);
        img_ack= (ImageView) findViewById(R.id.health_education_img_back);
        sp = getSharedPreferences("login", Context.MODE_PRIVATE);
        //创建选择器
        mpv1 = new TimePickerView(this, TimePickerView.Type.YEAR_MONTH_DAY);
        //设置时间范围
        Calendar calendar = Calendar.getInstance();
        mpv1.setRange(calendar.get(Calendar.YEAR) - 100, calendar.get(Calendar.YEAR) + 100);
        //插入时间
        mpv1.setTime(new Date());
        //选择监听
        mpv1.setOnTimeSelectListener(new TimePickerView.OnTimeSelectListener() {

            @Override
            public void onTimeSelect(Date date) {
                date1 = date;
                stime=getTime(date);
                stimetv.setText(stime);
            }
        });
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mpv1.show();
            }
        });
        mpv2 = new TimePickerView(this, TimePickerView.Type.YEAR_MONTH_DAY);
        //设置时间范围
        Calendar calendar1 = Calendar.getInstance();
        mpv2.setRange(calendar1.get(Calendar.YEAR) - 100, calendar1.get(Calendar.YEAR) + 100);
        //插入时间
        mpv2.setTime(new Date());
        //选择监听
        mpv2.setOnTimeSelectListener(new TimePickerView.OnTimeSelectListener() {

            @Override
            public void onTimeSelect(Date date) {
                date2 = date;
                etime=getTime(date);
                etimetv.setText(etime);
            }
        });
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mpv2.show();
            }
        });
        img_ack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        img_commit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        dockDao dockDao = new dockDaoImpl();
                        SC sc = new SC();
                        JSONObject object =sc.storage(sp);
                        try {
                            object.put("KXA02",edit_name.getText().toString());
                            object.put("KXA03",edit_jg.getText().toString());
                            object.put("KXA05",edit_bm.getText().toString());
                            object.put("KXA08",edit_pnm.getText().toString());
                            object.put("KXA15",edit_manger.getText().toString());
                            object.put("CID03",edit_cid.getText().toString());
                            object.put("CTS01A",stime);
                            object.put("CTS01B",etime);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        String json = object.toString();
                        GetCoorData.create();
                        GetCoorData.dockingEntityList= dockDao.queryAllCoordinationMessage(baseClass.find_coordination_url+1, json);
                        if ( GetCoorData.dockingEntityList.size() != 0) {
                                finish();

                        }else{
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(SearchCoordinationActivity.this, "此条件下查不到信息", Toast.LENGTH_SHORT).show();
                                }
                            });

                        }
                    }
                }).start();
            }
        });
    }
    public static String getTime(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(date);
    }
}
